{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd    \n",
    "import numpy as np \n",
    "\n",
    "import pyecharts.options as opts\n",
    "from pyecharts.globals import ThemeType\n",
    "from pyecharts.charts import Line,Bar,Map,Pie,Grid,Tab,Timeline\n",
    " \n",
    "from pyecharts.globals import CurrentConfig, NotebookType\n",
    "CurrentConfig.NOTEBOOK_TYPE = NotebookType.JUPYTER_LAB \n",
    "\n",
    "df_confirmed = pd.read_csv('data/world_confirmed.csv',\n",
    "                           header=0, index_col=[1, 0]).drop(['Lat', 'Long'], axis=1)\n",
    "df_death = pd.read_csv('data/world_death.csv',\n",
    "                       header=0, index_col=[1, 0]).drop(['Lat', 'Long'], axis=1)\n",
    "df_recovery = pd.read_csv('data/world_recovery.csv',\n",
    "                          header=0, index_col=[1, 0]).drop(['Lat', 'Long'], axis=1)\n",
    "\n",
    "sum_confirmed = df_confirmed.groupby('Country/Region').sum()\n",
    "sum_death = df_death.groupby('Country/Region').sum()\n",
    "sum_recovery = df_recovery.groupby('Country/Region').sum()\n",
    " "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "name_map = {\n",
    "    \"US\": \"United States\",\n",
    "    \"Korea, South\":\"Korea\",\n",
    "    \"Congo (Brazzaville)\":\"Congo\",\n",
    "    \"Congo (Kinshasa)\":\"Dem. Rep. Congo\",\n",
    "    \"Czechia\":\"Czech Rep.\",\n",
    "    \"North Macedonia\":\"Macedonia\",\n",
    "    \"Bosnia and Herzegovina\":\"Bosnia and Herz.\",\n",
    "    \"Laos\":\"Lao PDR\",\n",
    "    \"Dominica\":\"Dominican Rep.\",\n",
    "    \"Central African Republic\":\"Central African Rep.\",\n",
    "    \"South Sudan\":\"S. Sudan\",\n",
    "    \"Burma\":\"Myanmar\",\n",
    "    \"Cote d'Ivoire\":\"Côte d'Ivoire\"\n",
    "}\n",
    "\n",
    "\n",
    "def get_nation_name(name):\n",
    "    if name in name_map:\n",
    "        return name_map[name]\n",
    "    return name\n",
    "\n",
    "visual_opts = opts.VisualMapOpts(max_=5000000, is_piecewise=True,\n",
    "                                 pieces=[\n",
    "                                     {\"max\": 50000000, \"min\": 100000,\n",
    "                                      \"label\": \">100000\", \"color\": \"#6A0C0C\"},\n",
    "                                     {\"max\": 99999, \"min\": 10000,\n",
    "                                      \"label\": \"10000-99999\", \"color\": \"#800808\"},\n",
    "                                     {\"max\": 9999, \"min\": 5000,\n",
    "                                      \"label\": \"5000-9999\", \"color\": \"#FF0000\"},\n",
    "                                     {\"max\": 4999, \"min\": 500,\n",
    "                                      \"label\": \"500-4999\", \"color\": \"#B8860B\"},\n",
    "                                     {\"max\": 499, \"min\": 10,\n",
    "                                      \"label\": \"10-499\", \"color\": \"#EEB422\"},\n",
    "                                     {\"max\": 9, \"min\": 1, \"label\": \"1-9\",\n",
    "                                      \"color\": \"#EEDD82\"},\n",
    "                                     {\"max\": 0, \"min\": 0, \"label\": \"0\",\n",
    "                                      \"color\": \"#FFFFFF\"},\n",
    "                                 ])\n",
    " \n",
    "tl = Timeline()\n",
    "for day in sum_confirmed.columns:  \n",
    "    s = sum_confirmed[day]\n",
    "    pairlst = [(get_nation_name( s.index[i]),int(s.values[i])) for i in range(len(s))]\n",
    "    map0 = (\n",
    "        Map(init_opts=opts.InitOpts(theme=ThemeType.DARK))\n",
    "        .add(\"confirmed case\",pairlst, \"world\",is_map_symbol_show=False,label_opts=opts.LabelOpts(is_show=False))\n",
    "        .set_global_opts(visualmap_opts=visual_opts) \n",
    "    )\n",
    "    tl.add(map0, day) \n",
    "\n",
    "tl.load_javascript()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "tl.render_notebook()  "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "total_confirmed = sum_confirmed[sum_confirmed.columns[-1]]\n",
    "total_death = sum_death[sum_death.columns[-1]]\n",
    "total_recovery = sum_recovery[sum_recovery.columns[-1]]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "top_confirmed = total_confirmed.nlargest(20)\n",
    "top_confirmed.set_value('others',total_confirmed.sum()-top_confirmed.sum())\n",
    "top_death = total_death.nlargest(20)\n",
    "top_death.set_value('others',total_death.sum()-top_death.sum())\n",
    "\n",
    "# pie chart of confirmed cases\n",
    "pie = Pie(init_opts=opts.InitOpts(theme=ThemeType.DARK))\n",
    "pie_data = [(top_confirmed.index[i],int(top_confirmed.values[i])) for i in range(len(top_confirmed))]\n",
    "pie.add('Confirmed Cases', data_pair=pie_data)\n",
    "pie.set_global_opts(legend_opts=opts.LegendOpts(is_show=False)) \n",
    "\n",
    "# pie chart of death toll\n",
    "pie_death = Pie(init_opts=opts.InitOpts(theme=ThemeType.DARK))\n",
    "pie_death_data = [(top_death.index[i],int(top_death.values[i])) for i in range(len(top_death))]\n",
    "pie_death.add('Fatality toll', data_pair=pie_death_data)\n",
    "pie_death.set_global_opts(legend_opts=opts.LegendOpts(is_show=False))\n",
    "\n",
    "# bar chart of confirmed cases\n",
    "bar = Bar(init_opts=opts.InitOpts(theme=ThemeType.DARK)) \n",
    "bar_data = [(top_confirmed.index[i],int(top_confirmed.values[i])) for i in range(len(top_confirmed))].reverse()\n",
    "x = top_confirmed.index.tolist()[::-1] \n",
    "bar.add_xaxis(x).reversal_axis()\n",
    "y = (top_confirmed.values.tolist())[::-1] \n",
    "bar.add_yaxis('Confirmed Cases',y,label_opts=opts.LabelOpts(position=\"right\"))  \n",
    "bar.set_global_opts(legend_opts=opts.LegendOpts(is_show=False))\n",
    "print(x,y)\n",
    "\n",
    "# bar chart of death toll\n",
    "bar_death = Bar(init_opts=opts.InitOpts(theme=ThemeType.DARK)) \n",
    "bar_death_data = [(top_death.index[i],int(top_death.values[i])) for i in range(len(top_death))].reverse()\n",
    "bar_death.add_xaxis(top_death.index.tolist()[::-1]).reversal_axis()\n",
    "bar_death.add_yaxis('Fatality toll',top_death.values.tolist()[::-1],label_opts=opts.LabelOpts(position=\"right\"))\n",
    "\n",
    "bar_death.set_global_opts(legend_opts=opts.LegendOpts(is_show=False))\n",
    "\n",
    "tab =  Tab() \n",
    "tab.add(pie,\"Confirmed Cases\")\n",
    "tab.add(pie_death,\"Fatality toll\")\n",
    "tab.add(bar,\"Confirmed Cases\")\n",
    "tab.add(bar_death,\"Fatality toll\")\n",
    "\n",
    "tab.load_javascript()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "tab.render_notebook() "
   ]
  }
 ],
 "metadata": {
  "file_extension": ".py",
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.3"
  },
  "mimetype": "text/x-python",
  "name": "python",
  "npconvert_exporter": "python",
  "pygments_lexer": "ipython3",
  "version": 3
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
